﻿
function findLibExe()
{
    $pathtotest = $env:VS120COMNTOOLS
    if ($pathtotest -ne $Null)
    {
        $pathtotest = Join-Path $pathtotest ..\..\VC\bin
        if (Test-Path (Join-Path $pathtotest "lib.exe"))
        {
            return $pathtotest
        }
    }

    $pathtotest = $env:ProgramFiles
    if ($pathtotest -ne $Null)
    {
        $pathtotest = Join-Path $pathtotest "Microsoft Visual Studio 12.0\VC\bin"
        if (Test-Path (Join-Path $pathtotest "lib.exe"))
        {
            return $pathtotest
        }
    }
    
    $pathtotest = ${env:ProgramFiles(x86)}
    if ($pathtotest -ne $Null)
    {
        $pathtotest = Join-Path $pathtotest "Microsoft Visual Studio 12.0\VC\bin"
        if (Test-Path (Join-Path $pathtotest "lib.exe"))
        {
            return $pathtotest
        }
    }

    Write-Output "Couldn't determine where lib.exe can be found. Please provide the path in the script."
    # return  "...."
    Exit
}

function getfftwlibrary([string]$filename,[string] $folderName)
{
    New-Item -ItemType Directory -Force -Path (Join-Path $DownloadsPath $folderName)
    $file = Join-Path (Join-Path $DownloadsPath $folderName) $filename 

    # download the file (version 2.2 is current at the time of writing)
    $result = $wc.DownloadFile("ftp://ftp.fftw.org/pub/fftw/"+$filename,$file)

    # unzip the file
    $shell = new-object -com shell.application
    $zip = $shell.NameSpace($file)
    foreach($item in $zip.items())
    {
        $shell.Namespace((Join-Path $DownloadsPath $folderName)).copyhere($item)
    }

    $folderToCopy = (Join-Path $DownloadsPath $folderName)
    $destFolder = Join-Path (Join-Path $WorkPath "..\FTLib\fftw\") $folderName

    # make destination folder (if it does not exists) and copy the extracted files into this
    New-Item -ItemType Directory -Force -Path $destFolder 

    Copy-Item (Join-Path $folderToCopy "*.dll") $destFolder -Recurse
    Copy-Item (Join-Path $folderToCopy "*.def") $destFolder -Recurse

    Set-Location $destFolder

    $libExe = Join-Path $PathVisualStudio "lib.exe"

    & $libExe /MACHINE:$folderName /def:libfftw3-3.def
    & $libExe /MACHINE:$folderName /def:libfftw3f-3.def
    & $libExe /MACHINE:$folderName /def:libfftw3l-3.def
}

#$local = Get-Location;

# get the path where this script resides
$WorkPath = split-path $MyInvocation.MyCommand.Definition -Parent

# that's the folder where we want to place the downloads and extract the ZIPs
$DownloadsPath = Join-Path $WorkPath "Downloads"

# empty and remove folder if it exists, then create a new one
If (Test-Path $DownloadsPath)
{
    Remove-Item $DownloadsPath -Force -Recurse
}

New-Item -ItemType Directory -Force -Path $DownloadsPath

$wc = New-Object system.Net.WebClient;

# Try to determine the path where LIB.EXE can be found.
# If it cannot be found, then the script will terminate.
$PathVisualStudio = findLibExe


getfftwlibrary "fftw-3.3.4-dll64.zip" "x64"
getfftwlibrary "fftw-3.3.4-dll32.zip" "x86"
